﻿package binarytree;

import common.BTNode;

public class 可视化的方式打印二叉树 {

	public static void printTree(BTNode root) {
		System.out.println("Binary Tree: ");

	}

	public static void printInOrder(BTNode root, int height, String to, int len) {
		if (root == null)
			return;
		printInOrder(root.right, height + 1, "v", len);
		String val = to + root.value + to;
		int lenM = val.length();
		int lenL = lenM / 2;
		int lenR = lenM - lenL;
		val = getSpace(lenL) + val + getSpace(lenR);
		System.out.println(getSpace(height * len) + val);
		printInOrder(root.left, height + 1, "^", len);
	}

	/**
	 * 生成value个空格组成的字符串
	 * 
	 * @param value
	 * @return
	 */
	public static String getSpace(int value) {
		String res = String.valueOf(value);
		String space = "";
		StringBuffer buf = new StringBuffer("");
		for (int i = 0; i < value; i++) {
			buf.append(space);
		}
		return buf.toString();
	}
}
